typedef ProductID = String;

/// cart model
class Cart {
  // all the items in the shopping cart, where:
  // -key: product id
  // -value: quantity
  final Map<ProductID, int> items;

  const Cart([this.items = const {}]);
}

/// Helper extension used to mutate the items in the shopping cart
extension MutableCart on Cart {
  Cart addItem(Item item) {
    items.putIfAbsent(item.productId, () => item.quantity);
    return Cart(items);
  }

  Cart setItem(Item item) {
    items[item.productId] = item.quantity;
    return Cart(items);
  }

  Cart removeItemById(ProductID productId) {
    items.remove(productId);
    return Cart(items);
  }
}

/// a product along with a quantity that can be added to an order/cart
class Item {
  final ProductID productId;
  final int quantity;

  const Item({required this.productId, required this.quantity});
}
